#%%
import tushare as ts
from DataGeter import inputfomating

#%%


def TradeDays():
    """
    pro = 获取api借口的token https://tushare.pro/可以获取
    method = 是否需要获取新的交易日数据
    """
    # getTradeDays(methods="load")
    pro = ts.pro_api("c6bd0699784af91a642f2dae0f863de4ee93dc4db137723dc9f9a6dc")
    df = pro.trade_cal()
    return df


# %%


def getStockList():
    pro = ts.pro_api("c6bd0699784af91a642f2dae0f863de4ee93dc4db137723dc9f9a6dc")
    df = pro.query(
        "stock_basic",
        exchange="",
        list_status="L",
        fields="ts_code,name,exchange,market,area,industry,list_date,enname,list_status,is_hs",
    )

    return df


#%%----------------------------------------------------------------
def todays_ticks(code):
    code = inputfomating.codeFormating(code)
    df = ts.get_today_ticks(code)
    return df


# todays('601398')
#%%


def get_k_data(code, start="190012110101", end="20201209", ktype="D"):
    """
    + code = 股票代码
    + start 到 end = 交易日日期 "19000101"
    + ktype = [D:日线， W:周线， M:月线， 5:5分钟， 15:15分钟， 30， 60]
    """
    code = inputfomating.codeFormating(code)
    if start == None or end == None or start == end:
        df = ts.get_k_data(code=code, autype="None", ktype=ktype)
        print(code, start)
    else:
        start = inputfomating.dateFormating(start, mode="S")
        end = inputfomating.dateFormating(end, mode="S")
        df = ts.get_k_data(code=code, start=start, end=end, autype="None", ktype=ktype)
    # df.to_csv("tables/TradeHistory/{}_{}_{}.csv".format(code, ktype, end))
    return df


def getTradeDetail(code=300999, date="2020-12-11"):
    """
    code = 300999 str/int
    date = "2020-12-11"
    """
    code = inputfomating.codeFormating(code)
    date = inputfomating.dateFormating(date, mode="L")

    if len(date) == 8:
        y = date[:4]
        m = date[4:6]
        d = date[6:]
        date = "{}-{}-{}".format(y, m, d)
    elif len(date) > 10:
        print("日期格式有误")
    df = ts.get_tick_data(
        code,
        date=date,
        #   src='tt'
    )
    return df


if __name__ == "__main__":
    # getTradeDetail(code=300999, date="2020-12-11")
    df = get_k_data(601398, start="19000101", end=None, ktype="15")
    print(df)
    # todays_ticks(601398)